﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Wix.Aura.Common.CustomTypes;
using System.Xml.Serialization;
using System.Globalization;

namespace Wix.Aura.Common {
	public static class XmlWriterWixExtensions {

		public static void WriteAttribute(this XmlWriter writer, string localName, Language value) {
			writer.WriteAttribute(localName, (int)value);
		}

		public static void WriteAttribute<T>(this XmlWriter writer, string localName, T value) {
			if (value != null)
				writer.WriteAttributeString(localName, value.ToString());
		}

		public static void WriteAttribute(this XmlWriter writer, string localName, Context? value) {
			if (value != null)
				writer.WriteAttributeString(localName, value.Value.ToString().Replace(",",string.Empty));
		}

		public static void WriteAttribute(this XmlWriter writer, string localName, RegistryRoot value) {
			string xmlValue;
			switch(value){
				default: throw new NotSupportedException(value.ToString());
				case RegistryRoot.ClassesRoot:
					xmlValue = "HKCR";
					break;
				case RegistryRoot.CurrentUser:
					xmlValue = "HKCU";
					break;
				case RegistryRoot.LocalMachine:
					xmlValue = "HKLM";
					break;
				case RegistryRoot.Users:
					xmlValue = "HKU";
					break;
			}
			writer.WriteAttributeString(localName, xmlValue);
		}

		public static void WriteAttribute(this XmlWriter writer, string localName, LongFileNameType value) {
			writer.WriteAttribute(localName, value.Value);
		}
	}

}
